package mba.studies.read_system;

import mba.studies.read_system.entity.*;
import mba.studies.read_system.mapper.HksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.lang.Module;
import java.util.List;

@RestController
@RequestMapping("/hksEducation")
public class HksEducationController {


    @Autowired
    private HksMapper hksMapper;

    @GetMapping(value = "/banner")
    public List<Banners> banner() {
        return hksMapper.listAllBanners();
    }

    @GetMapping(value = "/modules")
    public List<MyModule> modules() {
        return hksMapper.listAllModules();
    }

    //{
    //  "newType":"l 全部 2 政策 3资讯"
    //}
    @GetMapping(value = "/news", name = "按类型返回新闻")
    public List<News> hello(@RequestParam(value = "newType", required = false) int newType) {
        return hksMapper.listAllNews(newType);
    }

    //{
    //  "newType":"l 全部 2 政策 3资讯"
    //}
    @GetMapping(value = "/schoolList", name = "按类型返回学校")
    public List<School> schoolList(@RequestParam(value = "schoolType", required = false) int schoolType) {
        return hksMapper.listAll(schoolType);
    }

    @GetMapping(value = "/getAllschool", name = "按类型返回学校")
    public List<School> getAllschool() {
        return hksMapper.getAllSchool();
    }


    @GetMapping(value = "/getConsultSubmit", name = "返回所有提交的咨询")
    public List<ConsultSubmit> getConsultSubmit(@RequestParam(value = "page", required = false,defaultValue = "-1")int page,@RequestParam(value = "pageSize", required = false,defaultValue = "-1")int pageSize) {
        if (pageSize == -1 || page == -1) {
            return hksMapper.getAllConsultSubmit();
        }
        return hksMapper.getConsultSubmit(page,pageSize);
    }

    //{
    // "schoolCode":"学校代码"
    //}
    @GetMapping(value = "/schoolDetails", name = "按code返回学校详情")
    public School schoolDetails(@RequestParam(value = "schoolCode", required = false) String schoolCode) {
        return hksMapper.querySchoolByCode(schoolCode);
    }

    //{
    // "schoolCode":"学校代码"
    //}
    @GetMapping(value = "/queryUserByOpenId", name = "按code返回学校详情")
    public User queryUserByOpenId(@RequestParam(value = "openId", required = false) String openId) {
        return hksMapper.queryUserByOpenId(openId);
    }

    @GetMapping(value = "/deleteSchool", name = "按code返回学校详情")
    public String deleteSchool(@RequestParam(value = "schoolCode", required = false) String schoolCode) {
        try {
            hksMapper.deleteSchool(schoolCode);
            return "删除成功";
        }catch (Exception e) {
            return "删除失败";
        }
    }

    @GetMapping(value = "/deleteConsultSubmit", name = "按code返回学校详情")
    public String deleteConsultSubmit(@RequestParam(value = "id", required = false) int id) {
        try {
            hksMapper.deleteConsultSubmit(id);
            return "删除成功";
        }catch (Exception e) {
            return "删除失败";
        }
    }

    @Value("${upload.path}")
    private String uploadPath;

    @PostMapping(value = "/addSchool", name = "添加学校")
    public String schoolDetails(UploadSchool school) {
        System.out.println("收到参数" + school.toString());
        String schoolCode = school.getSchoolCode();
        School addSchool = new School();

        addSchool.setSchoolPhone(school.getSchoolPhone());
        addSchool.setTitle(school.getTitle());
        addSchool.setSchoolType(school.getSchoolType());
        addSchool.setLabel(school.getLabel());
        addSchool.setWebUrl(school.getWebUrl());
        addSchool.setSchoolCode(school.getSchoolCode());
        addSchool.setSchoolOverview(school.getSchoolOverview());

        try {
            // 获取文件名
            String fileName =  schoolCode + "_"  + school.getPicUrl().getOriginalFilename();
            // 指定保存路径
            String filePath = uploadPath + "/" + fileName;
            // 保存文件到本地
            school.getPicUrl().transferTo(new File(filePath));
            addSchool.setPicUrl(fileName);

            // 获取文件名
            fileName =  schoolCode + "_"  + school.getSchoolPic().getOriginalFilename();
            // 指定保存路径
            filePath = uploadPath + "/" + fileName;
            // 保存文件到本地
            school.getSchoolPic().transferTo(new File(filePath));
            addSchool.setSchoolPic(fileName);

            // 获取文件名
            fileName =  schoolCode + "_"  + school.getSchoolVideo().getOriginalFilename();
            // 指定保存路径
            filePath = uploadPath + "/" + fileName;
            // 保存文件到本地
            school.getSchoolVideo().transferTo(new File(filePath));
            addSchool.setSchoolVideo(fileName);

            // 获取文件名
            fileName =  schoolCode + "_"  + school.getSchoolPiiic().getOriginalFilename();
            // 指定保存路径
            filePath = uploadPath + "/" + fileName;
            // 保存文件到本地
            school.getSchoolPiiic().transferTo(new File(filePath));
            addSchool.setSchoolPiiic(fileName);


            // 获取文件名
            fileName =  schoolCode + "_"  + school.getStudentsGuide().getOriginalFilename();
            // 指定保存路径
            filePath = uploadPath + "/" + fileName;
            // 保存文件到本地
            school.getStudentsGuide().transferTo(new File(filePath));
            addSchool.setStudentsGuide(fileName);

            hksMapper.addSchool(addSchool);


            return "添加成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "添加失败";
        }
    }



    @GetMapping(value = "/consultSubmit", name = "添加学校")
    public String consultSubmit(ConsultSubmit consultSubmit) {
        System.out.println("收到参数" + consultSubmit.toString());
        try {
            hksMapper.addConsultSubmit(consultSubmit);
        }catch (Exception e) {
            return "添加失败";
        }

        return "添加成功";
    }

    @GetMapping(value = "/addUser", name = "添加学校")
    public String addUser(User user) {
        System.out.println("收到参数" + user.toString());
        try {
            hksMapper.addUser(user);
        }catch (Exception e) {
            return "fail" + e.toString();
        }

        return "success";
    }

    @GetMapping(value = "/schoolAssess", name = "添加学校")
    public String addSchoolAssess(SchoolAssess schoolAssess) {
        System.out.println("收到参数" + schoolAssess.toString());
        try {
            hksMapper.addSchoolAssess(schoolAssess);
        }catch (Exception e) {
            return "-1";
        }

        return "0";
    }


    @GetMapping(value = "/applyForGrade", name = "添加学校")
    public String applyForGrade(ApplyForGrade applyForGrade) {
        System.out.println("收到参数" + applyForGrade.toString());
        try {
            hksMapper.addApplyForGrade(applyForGrade);
        }catch (Exception e) {
            return "-1";
        }

        return "0";
    }


    //中考时间
    @GetMapping(value = "/examTime", name = "返回中考时间")
    public String schoolDetails() {
        KeyValuesEntity examTime = hksMapper.getKeyValue("examTime");
        return examTime.getValueString();
    }


    //中考时间
    @GetMapping(value = "/updateExamTime", name = "返回中考时间")
    public String updateExamTime(@RequestParam(value = "examTime", required = false) String  examTime) {
        try {
            hksMapper.updateKeyValue("examTime",examTime);
            return "更新成功";
        }catch (Exception e) {
            return "更新失败";
        }
    }

    //{
    // "schoolCode":"学校代码"
    //}
    //联系电话
    @GetMapping(value = "/contactPhone", name = "返回联系电话")
    public String contactPhone() {
        KeyValuesEntity examTime = hksMapper.getKeyValue("contactPhone");
        return examTime.getValueString();
    }

    //中考时间
    @GetMapping(value = "/updateContactPhone", name = "返回中考时间")
    public String updateContactPhone(@RequestParam(value = "contactPhone", required = false) String  contactPhone) {
        try {
            hksMapper.updateKeyValue("contactPhone",contactPhone);
            return "更新成功";
        }catch (Exception e) {
            return "更新失败";
        }
    }
}
